package cn.yanji.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.yanji.pojo.ContentRow;
import cn.yanji.service.ExcelService;
import cn.yanji.service.ExecuteService;
import cn.yanji.service.element.ChapterService;
import cn.yanji.service.element.ContentService;
import org.apache.commons.lang3.StringUtils;
import org.openqa.selenium.*;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.FileNotFoundException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.TimeUnit;

/**
 * @author yanji w2c17lin@qq.com
 * @since 2018/11/7 上午 09:16
 */
@Service
public class ExecuteServiceImpl implements ExecuteService, DisposableBean {

	private WebDriver webDriver;

	private @Autowired ChapterService chapterService;
	private @Autowired ContentService contentService;

	private @Autowired ExcelService excelService;

	@Autowired
	public ExecuteServiceImpl(WebDriver webDriver) {
		this.webDriver = webDriver;
		this.webDriver.manage().timeouts().implicitlyWait(1, TimeUnit.SECONDS);
//		this.webDriver.manage().timeouts().pageLoadTimeout(1, TimeUnit.SECONDS);
//		this.webDriver.manage().timeouts().setScriptTimeout(1, TimeUnit.SECONDS);

		// 通过加载一个当前域名的其它网页，设置COOKIE，如果不这样做的话是设置不进去COOKIE的
		this.go(PRE_HOME);
		Cookie cookie = new Cookie(SESSION_KEY, SESSION_VALUE, SESSION_DOMAIN, "/", null);
		this.webDriver.manage().addCookie(cookie);
		this.go(HOME);
	}

	@Override
	public WebDriver webDriver() {
		return this.webDriver;
	}

	@Override
	public WebElement element(String id) {
		return this.webDriver.findElement(By.id(id));
	}

	@Override
	public void click(String id) {
		Optional.ofNullable(webDriver.findElement(By.id(id))).ifPresent(WebElement::click);
	}

	@Override
	public void go(String url) {
		url = StringUtils.startsWith(url, "/") ? DOMAIN + url : DOMAIN + "/" + url;
		this.webDriver.get(url);
	}

	@Override
	public void back() {
		this.webDriver.navigate().back();
	}

	@Override
	public void quit() {
		this.webDriver.quit();
	}

	@Override
	public void exec() throws FileNotFoundException {
//		this.exec(ChapterService.SUBJECT_1, ChapterService.TYPE_0);
//		this.exec(ChapterService.SUBJECT_1, ChapterService.TYPE_1);
//		this.exec(ChapterService.SUBJECT_1, ChapterService.TYPE_2);
//		this.exec(ChapterService.SUBJECT_1, ChapterService.TYPE_3);

		this.exec(ChapterService.SUBJECT_4, ChapterService.TYPE_0);
		this.exec(ChapterService.SUBJECT_4, ChapterService.TYPE_1);
		this.exec(ChapterService.SUBJECT_4, ChapterService.TYPE_2);
//		this.exec(ChapterService.SUBJECT_4, ChapterService.TYPE_3);
		this.webDriver.quit();
	}

	@Override
	public void exec(String subject, String type) throws FileNotFoundException {
		Map<String, List<ContentRow>> data = new HashMap<>();
		int nowNum = 0;
		for (; ; ) {
			List<WebElement> chapters = chapterService.chapters(subject, type);
			if (CollectionUtil.isEmpty(chapters) || nowNum == chapters.size()) break;

			String chpaterName = StringUtils.trim(chapters.get(nowNum).getText());
			chapters.get(nowNum).click();
			this.closeAlert();
			List<ContentRow> contentRows = contentService.contents();
			data.put(chpaterName, contentRows);
			this.back();
			nowNum++;
		}
		excelService.write(subject + type + ".xlsx", data);
	}

	@Override
	public void destroy() {
		this.webDriver.quit();
	}

	private void closeAlert() {
		try {
			this.webDriver().switchTo().alert().dismiss();
		} catch (NoAlertPresentException ignored) {
		}
	}
}
